package cn.haoxsf.app.system.service.impl;

import cn.haoxsf.app.common.api.enums.SettledInExceptionEnum;
import cn.haoxsf.app.common.api.enums.SystemExceptionEnum;
import cn.haoxsf.app.common.exception.BaseException;
import cn.haoxsf.app.common.util.ToolUtil;
import cn.haoxsf.app.system.entity.FaultCategory;
import cn.haoxsf.app.system.entity.FileInfo;
import cn.haoxsf.app.system.entity.SettledIn;
import cn.haoxsf.app.system.mapper.SettledInMapper;
import cn.haoxsf.app.system.service.IAuthService;
import cn.haoxsf.app.system.service.IFaultCategoryService;
import cn.haoxsf.app.system.service.IFileInfoService;
import cn.haoxsf.app.system.service.ISettledInService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 入驻表 服务实现类
 * </p>
 *
 * @author Ele  qvbanfm@qq.com
 * @since 2020-03-15
 */
@Service
@Log4j2
public class SettledInServiceImpl extends ServiceImpl<SettledInMapper, SettledIn> implements ISettledInService {
    @Autowired
    private IAuthService authService;
    @Autowired
    private IFaultCategoryService faultCategoryService;
    @Autowired
    private IFileInfoService fileInfoService;

    /**
     * 保存
     *
     * @param settledIn 信息
     */
    @Override
    public void addSettledIn(SettledIn settledIn) {
        try {
            FaultCategory byId = faultCategoryService.getById(settledIn.getSettledInTypeId());
            if(ToolUtil.isEmpty(byId)){
                throw new BaseException(SystemExceptionEnum.SERVER_ERROR.getCode(),"入驻类型id错误！");
            }
            this.baseMapper.insert(settledIn);
        } catch (RuntimeException e) {
            log.info("用户重复", e.getMessage());
            throw new BaseException(SettledInExceptionEnum.USER_ALREADY_EXISTS);
        }

    }

    /**
     * 查询一跳信息
     *
     * @param memberId 用户id
     * @return 结果
     */
    @Override
    public Map<String, Object> getOne(String memberId) {
        SettledIn settledIn = baseMapper.selectOne(new QueryWrapper<SettledIn>().eq("member_id", memberId));
        if (ToolUtil.isEmpty(settledIn)) {
            throw new BaseException(SystemExceptionEnum.NOT_DATA);
        }
        Map<String, Object> maps = ToolUtil.beanToMap(settledIn);
        Long settledInTypeId = (Long) maps.get("settledInTypeId");
        String settledInTypeName = null;
        if (ToolUtil.isNotEmpty(settledInTypeId)) {
            settledInTypeName = faultCategoryService.getById(settledInTypeId).getCategoryName();
        }else {
            throw new BaseException(SystemExceptionEnum.SERVER_ERROR.getCode(),"系统错误，入驻类型id不能为空！");
        }
        if (ToolUtil.isEmpty(settledInTypeName)) {
            log.info("获取settledInTypeName失败[{}]", maps.get("settledInTypeId"));
            throw new BaseException(SystemExceptionEnum.SERVER_ERROR.getCode(), "获取数据失败！请联系管理员");
        }
        maps.put("username", authService.getLoginUser().getUsername());
        maps.put("settledInTypeName", settledInTypeName);
        if ("YES".equals(maps.get("isNotStore"))) {//有门店
            maps.put("storeLicense", fileInfoService.getOne(new QueryWrapper<FileInfo>().eq("final_name", maps.get("stoerLicense"))).getFilePath());
            maps.put("storeImage", fileInfoService.getOne(new QueryWrapper<FileInfo>().eq("final_name", maps.get("storeImageId"))).getFilePath());
        } else {//没有
            maps.put("storeLicense", null);
            maps.put("storeImage", null);
        }
        Object cardImageId = maps.get("cardImageId");
        if (!ToolUtil.isNotEmpty(maps.get("cardNo"))) {
            maps.put("cardNo", null);
        }
        if (ToolUtil.isNotEmpty(cardImageId)) {//身份证图片不为空时
            String[] cardImageIds = cardImageId.toString().split(",");//身份证图片
            if (ToolUtil.isNotEmpty(cardImageIds)) {
                maps.put("cardImageList", fileInfoService.selectBatchFinalNames(cardImageIds));
            }
        } else {
            maps.put("cardImageList", null);
        }
        maps.put("store", "YES".equals(maps.get("isNotStore")) ? "有" : "无");
        //删除s
        maps.remove("memberId");
        maps.remove("settledInTypeId");
        maps.remove("stoerLicense");
        maps.remove("isNotStore");
        maps.remove("storeImageId");
        maps.remove("cardImageId");
        return maps;
    }
}
